package Json;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.json.JSONException;
import org.json.JSONObject;
//import sun.nio.cs.ext.MS874;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Ajie
 * @date 2019-09-06
 * @function
 */
public class GsonTest2 {
    public static void main(String[] args) {
        String str ="{\"success\":false,\"errorMsg\":\"当前预约已过期\",\"errorCode\":\"4002\",\"model\":\"\",\"attributes\":null}";
//        CommonResponse<List<CheckAPIResponse>> bean = new Gson().fromJson(str, new TypeToken<CommonResponse<List<CheckAPIResponse>>>() {}.getType());

//        CommonResponse commonResponse = new Gson().fromJson(str, CommonResponse.class);
//        System.out.println("".equals(commonResponse.getModel()));

        String s = checkResponse(str);
        System.out.println(s);
        CommonResponse<List<CheckAPIResponse>> s1bean = new Gson().fromJson(s, new TypeToken<CommonResponse<List<CheckAPIResponse>>>() {}.getType());


//{"success":false,"errorMsg":"当前预约已过期","errorCode":"4002","model":null,"attributes":null}
        String make ="{\"success\":false,\"errorMsg\":\"当前预约已过期\",\"errorCode\":\"4002\",\"model\":null,\"attributes\":null}";
        CommonResponse<List<CheckAPIResponse>> bean = new Gson().fromJson(make, new TypeToken<CommonResponse<List<CheckAPIResponse>>>() {}.getType());
        ArrayList<CheckAPIResponse> checkAPIResponses = new ArrayList<>();
        CheckAPIResponse checkAPIResponse = new CheckAPIResponse();
        checkAPIResponse.setUsername("阿三");
        checkAPIResponse.setVisitorName(" 李小姐 ");
        checkAPIResponses.add(checkAPIResponse);
        bean.setModel(checkAPIResponses);

        String s1 = checkResponse(new Gson().toJson(bean));
        System.out.println("lsit "+s1);

        CommonResponse<List<CheckAPIResponse>> s1bean123 = new Gson().fromJson(s1, new TypeToken<CommonResponse<List<CheckAPIResponse>>>() {}.getType());
    }

    public static String checkResponse(String response) {
        System.out.println("check before "+response);
        if (response==null || response.length()==0){
            return null;
        }
        try {
            JSONObject obj = new JSONObject(response);
            if (obj.has("model")){
//                String model = obj.getString("model");
                Object mobj = obj.get("model");

                System.out.println("model to string "+ mobj.toString());

//                if ("".equals(commonResponse.getModel())){
//                System.out.println("equals " +"".equals(mobj));
                if ("".equals(mobj)){
//                if (mobj instanceof String && "".equals(mobj) || "".equals(model)){

                    CommonResponse commonResponse = new Gson().fromJson(response, CommonResponse.class);
                    System.out.println("setbefore \"\" "+commonResponse.getModel().equals(""));
                    commonResponse.setModel(null);
                    System.out.println("-----set null--"+(null == commonResponse.getModel()));
                    return new Gson().toJson(commonResponse);
                }else {
                    System.out.println("not have");
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return response;
    }
}
class CheckAPIResponse {

    /**
     * visitorName : 何二狗
     * attendeeCompany : 众安科技
     * visitStartTime : 2019-07-12 15:00
     * username : 何劭铭
     */

    private String visitorName;
    private String attendeeCompany;
    private String visitStartTime;
    private String username;

    public String getVisitorName() {
        return visitorName;
    }

    public void setVisitorName(String visitorName) {
        this.visitorName = visitorName;
    }

    public String getAttendeeCompany() {
        return attendeeCompany;
    }

    public void setAttendeeCompany(String attendeeCompany) {
        this.attendeeCompany = attendeeCompany;
    }

    public String getVisitStartTime() {
        return visitStartTime;
    }

    public void setVisitStartTime(String visitStartTime) {
        this.visitStartTime = visitStartTime;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}
